/* Copyright (c) 2010, Michael J. Allison

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, 
 this list of conditions and the following disclaimer.
 
 * Redistributions in binary form must reproduce the above copyright notice, 
 this list of conditions and the following disclaimer in the documentation 
 and/or other materials provided with the distribution.
 
 * Neither the name of Michael J. Allison nor the names of contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 THE POSSIBILITY OF SUCH DAMAGE.
 */

package xamcommons.objectinspector;

import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.Vector;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JRadioButton;

public class XRIDialog
   extends JDialog
   implements ActionListener
{
   final String CMD_COMBO_CHANGED = "*comboBoxChanged*";
   final String CMD_OK            = "*OK*";
   final String CMD_CANCEL        = "*Cancel*";
   final int    WIDTH = 320;
   final int    HEIGHT = 240;
   
   Vector<String> m_xri_list;
   JComboBox      m_xri_combo;
   boolean        m_is_ok = false;
   JRadioButton   m_anonymous;
   JRadioButton   m_plain;
   
   /**
    * System (XRI) and authentication style selection dialog. 
    * Presents an editable combo box with a the xri list, and allows 
    * the user to select ANONYMOUS/PLAIN authentication. 
    */
   public XRIDialog( Frame owner, Vector<String> xri_list )
   {
      super( owner, true );	
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      
      m_xri_list = xri_list;
      
      this.setSize( 320, 240 );
      Box vbox = Box.createVerticalBox();
      
      m_xri_combo = new JComboBox(xri_list);
      m_xri_combo.setEditable( true );
      m_xri_combo.setActionCommand( CMD_COMBO_CHANGED );
      m_xri_combo.addActionListener( this );
      vbox.add( m_xri_combo );
      
      // Authentication choice
      ButtonGroup grp = new ButtonGroup();
      m_anonymous = new JRadioButton( "Anonymous" );
      grp.add( m_anonymous );
      vbox.add( m_anonymous );
      
      m_plain     = new JRadioButton( "Plain" );
      m_plain.setSelected(true);
      grp.add( m_plain );
      vbox.add( m_plain );
      
      // OK/Cancel
      Box controls = Box.createHorizontalBox();
      JButton ok_button = new JButton( "OK" );
      ok_button.setActionCommand( CMD_OK );
      ok_button.addActionListener( this );
      controls.add( ok_button );
      
      JButton cancel_button = new JButton( "Cancel" );
      cancel_button.setActionCommand( CMD_CANCEL );
      cancel_button.addActionListener( this );
      controls.add( cancel_button );
      
      vbox.add( controls );
      this.add( vbox );
      
      this.setDefaultLookAndFeelDecorated( false );
      this.setResizable(false);
      this.pack();
      
      // Center on parent
      if( owner != null )
      {
         Rectangle r = owner.getBounds();
         Rectangle d = getBounds();
         int x = (int) ((r.getWidth() - d.getWidth()) / 2 + r.getX());
         int y = (int) ((r.getHeight() - d.getHeight()) / 2 + r.getY());
         setLocation( x, y );
      }
      
      this.setVisible(true);
   }
   
   /**
    * Return the selected XRI.
    */
   public String getXRI()
   {
      String xri = (String) m_xri_combo.getSelectedItem();
      if( ! m_xri_list.contains(xri) )
         m_xri_list.add(xri);
      return xri;
   }
   
   /**
    * Returns true when the OK button has been pressed, false otherwise. 
    */
   public boolean isOK()
   {
      return m_is_ok;
   }
   
   /**
    * Return true if PLAIN is selected, false otherwise. 
    */
   boolean isPlain()
   {
      return m_plain.isSelected();
   }
   
   /**
    * Return true if ANONYMOUS is selected, false otherwise.
    */
   boolean isAnonymous()
   {
      return m_anonymous.isSelected();
   }
   
   /**
    * Respond to dialog actions; close, edit XRI, etc. 
    */
   public void actionPerformed( ActionEvent e )
   {
      String command = e.getActionCommand();
      
      if( command.equals( CMD_COMBO_CHANGED ) )
      {
      }
      
      if( command.equals( CMD_OK ) )
      {
         m_is_ok = true;
         this.setVisible(false);
      }
      
      if( command.equals( CMD_CANCEL ) )
         this.setVisible(false);
   }
}
